import { PageType } from "../types/global";
import { FormType } from "../views/system/data/user";
import useUserStore from "../store/userStore";
import request from '../utils/request'
// 获取验证码
export const apiGetVCode = () => {
    return request({
        url: '/user/vcode',
        method: 'GET',
        noAuthed: true
    });
}

// 登录
export const apiAdminLogin = async (data: any) => {
    const { SET_TOKEN, vcodeToken } = useUserStore();
    const result: any = await request({
        url: '/user/login',
        method: 'post',
        data: data,
        headers: {
            Authorization: 'Bearer ' + vcodeToken //设置验证码
        }
    });
    const { token } = result;
    localStorage.setItem('token', token);
    SET_TOKEN(token);
    return { token }
}

// 注册，添加用户
export const apiAdminRegister = (data: FormType) => {
    return request({
        url: '/user/register',
        method: 'post',
        data: data
    });
}

// 获取用户信息
export const apiAdminInfo = () => {
    return request({
        url: '/user/info',
        method: 'get',
    });
}

// 编辑用户信息
export const apiUpdateAdminInfo = (data: FormType) => {
    return request({
        url: '/user/update',
        method: 'post',
        data
    });
}
// 删除后台用户
export const apiAdminDelete = (ids: any[]) => {
    return request({
        url: '/user/delete',
        method: 'post',
        data: { ids }
    });
}


// 分页获取用户列表
export const apiAdminPage = (data: PageType) => {
    return request({
        url: '/user/page',
        method: 'get',
        data: data
    });
}
